package com.ep.commonrecycleviewadapter.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by Administrator on 2017/8/1.
 */

public class MultiItemTypeAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    private Context mContext;
    private List<T> mDatas;

    private ItemViewDelegateManager mItemViewDelegateManager;
    private OnItemClickListener mOnItemClickListener;

    public MultiItemTypeAdapter(Context context) {
        mContext = context;
        mItemViewDelegateManager=new ItemViewDelegateManager();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(viewType);
        int layoutId=itemViewDelegate.getItemViewLayoutId();
        View itemView= LayoutInflater.from(mContext).inflate(layoutId,parent,false);
        ViewHolder viewHolder=new ViewHolder(mContext,itemView);
        setListener(viewHolder,parent,viewType);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        bindData(holder,mDatas.get(position));
    }


    @Override
    public int getItemCount() {
        return mDatas!=null?mDatas.size():0;
    }

    @Override
    public int getItemViewType(int position) {
        if (useItemViewDelegateManager()){
            return mItemViewDelegateManager.getItemViewType(mDatas.get(position),position);
        }
        return super.getItemViewType(position);
    }

    //---------------------------------自定义方法-----------------------------------------------------------------

    /**
     * 设置点击事件
     * @param viewHolder
     * @param parent
     * @param viewType
     */
    private void setListener(final ViewHolder viewHolder, ViewGroup parent, final int viewType) {
        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(v,viewHolder,viewHolder.getAdapterPosition());
                }
            }
        });

        viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener!=null){
                    return mOnItemClickListener.onItemLongClick(v,viewHolder,viewHolder.getAdapterPosition());
                }
                return false;
            }
        });
    }
    /**
     *
     * @param holder
     * @param t
     */
    private void bindData(ViewHolder holder, T t) {
        mItemViewDelegateManager.bindData(holder,t,holder.getAdapterPosition());
    }

    /**
     *
     * @param itemViewDelegate
     * @return
     */
    public MultiItemTypeAdapter addItemViewDelegate(ItemViewDelegate<T> itemViewDelegate) {
        mItemViewDelegateManager.addItemViewDelete(itemViewDelegate);
        return this;
    }

    /**
     *
     * @return
     */
    protected boolean useItemViewDelegateManager() {
        return mItemViewDelegateManager.getItemViewDelegateCount() > 0;
    }
    /**
     * 设置数据源
     */
    public void setDatas(List<T> datas){
        this.mDatas = datas;
        this.notifyDataSetChanged();
    }

    /**
     * 追加数据
     * @param datas
     */
    public void addDatas(List<T> datas){
        this.mDatas.addAll(datas);
        this.notifyDataSetChanged();
    }

    /**
     * 删除数据源
     * @param position
     */
    public void deleteDatas(int position){
        this.mDatas.remove(position);
        this.notifyDataSetChanged();
    }

    /**
     * 对外提供设置点击事件方法
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        mOnItemClickListener=onItemClickListener;
    }
    //----------------------------------点击事件接口------------------------------------------------
    public interface OnItemClickListener{
        void onItemClick(View view,RecyclerView.ViewHolder viewHolder,int position);
        boolean onItemLongClick(View view,RecyclerView.ViewHolder viewHolder,int position);
    }
}
